import cTitle from "components/title";
import { Toast } from 'vant';
import diyBackGround from "../member/balance/componet/diyBackGround.vue";

var isclick = true;

export default {
  data() {
    return {
      //爱心转账值
      sell_value: "",
      //爱心会员ID
      sell_id: "",
      // 转让手续费比率
      poundage: 0,
      trading_limit: 0,
      trading_fold: 0,

      love_name: "", //爱心值自定义名称
      usable: 0, // 登陆会员可用爱心值
      rate: 0,

      isTransformLoveV: false, //判断是否为转化爱心值
      balanceLang: "" ,//金额自定义
      // YCC转换 冻结爱心值的自定义名称
      froze_love_name:'',
    };
  },
  computed:{
    tips_C(){
      if(this.$route.name == 'YccformFrozenLove'){
        return "确认转化"
      }
      return `确认${this.isTransformLoveV?"出售" : "转化"}`
    },
    contenTxt(){
      if(this.$route.name == 'YccformFrozenLove'){
        return "全部兑换"
      }
      return this.isTransformLoveV ? "全部出售" : "全部兑换"
    },
    setPageTitle(){
      if(this.$route.name == 'YccformFrozenLove'){
        return `转换${this.froze_love_name}`
      }else if(this.isTransformLoveV){
        return `${this.love_name}出售`
      }else{
        return `转化${this.love_name}`
      }
    },
    setPriceIs(){
      if(this.$route.name == 'YccformFrozenLove'){
        return `可转让`
      }else if(this.isTransformLoveV){
        return `可出售${this.love_name}`
      }else{
        return `可出售${this.balanceLang}`
      }
    },
    setContenTxt(){
      if(this.$route.name == 'YccformFrozenLove'){
        return `全部转让`
      }else if(this.isTransformLoveV){
        return `全部出售`
      }else{
        return `全部兑换`
      }
    }
  },
  methods: {
    allConversion(){
      this.sell_value = this.usable;
    },
    getUsable() {
      let url = "";
      if (this.$route.name == 'YccformFrozenLove'){
        url = "plugin.ycc-money.frontend.index.index";
      }else if(this.isTransformLoveV){
        url = "plugin.love.Frontend.Controllers.page.index";
      }else{
        url = "finance.balance.conver";
      }
      $http
        .get(url, {}, "加载中")
        .then(
          response => {
            if (response.result === 1) {
              // this.usable = response.data.usable;
              if (this.$route.name == 'YccformFrozenLove'){
                let basic_info = JSON.parse(localStorage.getItem("yz_basic_info"))
                this.froze_love_name = basic_info.plugin_setting.love.froze_love_name;
                this.fun.setWXTitle(`转换${basic_info.plugin_setting.love.froze_love_name}`)
                this.usable = response.data.plugin_member.amount;
                this.rate = response.data.exchange_frozen_love_double;
              }else if(this.isTransformLoveV){
                this.love_name = response.data.love_name;
                this.fun.setWXTitle(this.love_name + "出售");
              }else{
                this.usable = response.data.credit2;
                this.rate = response.data.rate;
              }
              
            } else {
              this.$dialog.alert({message:response.msg});
              
            }
          }
        );
    },
    getLove() {
      if(this.$route.name == 'YccformFrozenLove') return
      if (this.$store.state.temp.designer && !this.fun.isTextEmpty(this.$store.state.temp.designer.love_name) ) {
        this.love_name = this.$store.state.temp.designer.love_name; //获取爱心值自定义从全局
        this.fun.setWXTitle("转化" + this.love_name);
      }

      if (this.isTransformLoveV) {
        $http
          .get(
            "plugin.love.Frontend.Modules.Trading.Controllers.trading.get-sell-love",
            {},
            "加载中"
          )
          .then(
            response => {
              if (response.result === 1) {
                this.usable = response.data.love.usable;
                this.poundage = response.data.set.poundage;
                this.trading_limit = response.data.set.trading_limit;
                this.trading_fold = response.data.set.trading_fold;
                if(!this.fun.isTextEmpty(response.data.set.trading_fetter)){
                  this.trading_fetter = response.data.set.trading_fetter;
                }
              } else {
                this.$dialog.alert({message:response.msg});
              }
            },
            function(response) {
              this.$dialog.alert({message:response.msg});
            }
          );
      }
    },
    allConversion(){
      this.sell_value = this.usable ;
    },
    saleBalance() {
      if (this.isTransformLoveV && !this.sell_value && this.$route.name != 'YccformFrozenLove') {
        Toast("请输入您要出售的数额");
        return;
      }
      let url = 'finance.balance.convert-love-value';
      let json = {};

      if(isclick) {
        isclick= false;
        if(this.$route.name == 'YccformFrozenLove'){
          url = "plugin.ycc-money.frontend.exchange.exchange";
          json = {
            amount: this.sell_value
          }
        }else if(this.isTransformLoveV){
          url = "plugin.love.Frontend.Modules.Trading.Controllers.trading.save-sell-love";
          json = {
            amount: this.sell_value
          }
        }else{
          url = "finance.balance.convert-love-value";
          json = {
            convert_amount: this.sell_value
          }
        }
        $http.get(url, json, ".").then(
            response => {
              setTimeout(function(){
                isclick = true;
              }, 1000);
              if (response.result === 1) {
                this.$dialog.alert({message:response.msg});
                this.sell_value = "";
                if(this.isTransformLoveV && this.$route.name != 'YccformFrozenLove'){
                  this.getLove();
                }else {
                  this.getUsable();
                }
              } else {
                this.$dialog.alert({message:response.msg});
              }
              
            },
            function(response) {
              if(this.isTransformLoveV){
                setTimeout(function(){
                  isclick = true;
                }, 1000);
                this.$dialog.alert({message:response.msg});
              }
            }
          );
      }

    }
  },
  activated() {
    this.balanceLang = this.fun.getBalanceLang(); //获取金额自定义
    this.isTransformLoveV = this.$route.name != "transformLoveV" ? true : false;
    console.log(this.isTransformLoveV,'ddddddddd')
    this.getUsable();
    this.getLove();
  },
  components: { cTitle,diyBackGround }
};
